package awesomeDocumentIO;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import misc.Vals;

import document.AwesomeDocument;

/**
 * Used to import document type information to the files system.
 * @author Brittin
 *
 */
public class AwesomeDocumentImporter {
	
	
	/**
	 * Returns the document associated with the given label.
	 * @param label the unique name of the document type.
	 * @return the document associated with the given label.
	 */
	public static AwesomeDocument importDocumentType(String label) {
		
		//String docfilepath = Vals.docDirectoryPath + File.separator + label + Vals.docFileExt;
		String dictfilepath = Vals.docDirectoryPath + File.separator + label + Vals.docDictExt;
		
		AwesomeDocument doc = readDocFromDir(label);
		if ( doc == null )
			return null;
		
		HashMap<String, Color> dict = readDictFromFile(dictfilepath);
		if ( dict == null )
			return null;
		
		doc.setLibrary(dict);
		
		return doc;
	}
	/**
	 * Returns the document created from the information in the directory file.
	 * The document that is returned does not include the dictionary associated with the doc type.
	 * @return the document created from the information in the directory file.
	 */
	private static AwesomeDocument readDocFromDir(String label) {
		AwesomeDocument doc = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(Vals.docDirectoryFile));
			doc = new AwesomeDocument();
			String line = null;
			
			while ( !((line = reader.readLine())  == null)) {
				
				// Look for the right label
				if (line.contains(label)) {
					
					doc.setLabel(line);
					line = reader.readLine();
					doc.setExtension(line);
				} else {
					line = reader.readLine();
				}
					
			}
			
			reader.close();
			
			if (doc.getLabel() == null || doc.getExtension() == null)
				return null;
			
			
			return doc;
		} catch (IOException ex) {
			System.err.println(ex);
			return doc;
		}
	}
	
	/**
	 * Returns the document created from the information in the file at the given file path.
	 * The document that is returned does not include the dictionary associated with the doc type.
	 * @param filepath the path of the file to use.
	 * @return the document created from the information in the file at the given file path.
	 */
	private static AwesomeDocument readDocFromFile(String filepath) {
		AwesomeDocument doc = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(filepath));
			doc = new AwesomeDocument();
			String line = reader.readLine();

			// Check for improperly formatted file.
			if ( !line.equals(Vals.docFileHeader) ) {
				return null;
			}
			
			while ( !(line = reader.readLine()).equals(Vals.docFileFooter) ) {
				if (line.contains(Vals.docLabelTab))
					doc.setLabel(line.substring(Vals.docLabelTab.length() + 1));
				if (line.contains(Vals.docFileExt))
					doc.setLabel(line.substring(Vals.docFileExt.length() + 1));
			}
			
			reader.close();
			
			if (doc.getLabel() == null || doc.getExtension() == null)
				return null;
			
			return doc;
		} catch (IOException ex) {
			System.err.println(ex);
			return doc;
		}
	}
	
	/**
	 * Returns the dictionary located at the given file path.
	 * @param filepath the path of the file to use.
	 * @return the dictionary located at the given file path.
	 */
	public static HashMap<String, Color> readDictFromFile(String filepath) {
		HashMap<String, Color> dict = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(filepath));
			dict = new HashMap<String, Color>();
			
			String line = reader.readLine();
			
			// Check for improperly formatted file.
			if ( !line.equals(Vals.docFileHeader) ) {
				return null;
			}
			String key;
			String colorstring;
			Color color;
			while ( !(line = reader.readLine()).equals(Vals.docFileFooter) ) {
				key = line.substring(Vals.docLibraryKeyTab.length() + 1);
				line = reader.readLine();
				colorstring = line.substring(Vals.docLibraryColorTab.length() + 1);
				color = getColorFromString(colorstring);
				dict.put(key, color);
			}
			
			reader.close();
		} catch (IOException ex) {
			System.err.println(ex);
		}
		return dict;
	}
	
	/**
	 * Returns the color with the r,g,b values in the passed string.
	 * @param colorinfo r,g,b values. Formatted as r=255,g=175,b=175
	 * @return the color with the r,g,b values in the passed string.
	 */
	private static Color getColorFromString(String colorinfo) {
		// color details will be in the format r=255,g=175,b=175
		// We must extract the values for r, g, b
		String[] values = colorinfo.split(",*.=");
		Color retVal = new Color(Integer.valueOf(values[1]), Integer.valueOf(values[2]), Integer.valueOf(values[3]));
		
		return retVal;
	}
	
	/**
	 * Returns a mapping of document labels to extensions.
	 * @return a mapping of document labels to extensions.
	 */
	public static HashMap<String, String> getAllDocInfo() {
		HashMap<String, String> docs = new HashMap<String, String>();
		
		try {
			// Open the directory and get all the labels
			BufferedReader reader = new BufferedReader(new FileReader(Vals.docDirectoryFile));
			
			String line = null;
			String extension = null;
			while ( !((line = reader.readLine()) == null )) {
				extension = reader.readLine();
				docs.put(line, extension);
			}
			
			reader.close();
			
			return docs;
		} catch (IOException ex) {
			System.err.println(ex);
			return null;
		}
	}
	
}
